<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="pharmacyname">
        <el-input
          v-model="queryParams.pharmacyname"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取单位序号" prop="drugstore">
        <el-input
          v-model="queryParams.drugstore"
          placeholder="请输入领取单位序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经办人" prop="skull">
        <el-input
          v-model="queryParams.skull"
          placeholder="请输入经办人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提取仓库" prop="warehouseid">
        <el-input
          v-model="queryParams.warehouseid"
          placeholder="请输入提取仓库"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售价" prop="sellingprice">
        <el-input
          v-model="queryParams.sellingprice"
          placeholder="请输入售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产地" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入产地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="producedate">
        <el-date-picker clearable
          v-model="queryParams.producedate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期" prop="validdate">
        <el-date-picker clearable
          v-model="queryParams.validdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="数量" prop="drugstorenum">
        <el-input
          v-model="queryParams.drugstorenum"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="skullbatch">
        <el-input
          v-model="queryParams.skullbatch"
          placeholder="请输入批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:pharmacy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:pharmacy:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:pharmacy:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:pharmacy:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pharmacyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="pharmacyid" />
      <el-table-column label="药品名称" align="center" prop="pharmacyname" />
      <el-table-column label="领取单位序号" align="center" prop="drugstore" />
      <el-table-column label="经办人" align="center" prop="skull" />
      <el-table-column label="提取仓库" align="center" prop="warehouseid" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="售价" align="center" prop="sellingprice" />
      <el-table-column label="产地" align="center" prop="area" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="生产日期" align="center" prop="producedate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.producedate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="validdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="drugstorenum" />
      <el-table-column label="批号" align="center" prop="skullbatch" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pharmacy:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pharmacy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药房对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="pharmacyname">
          <el-input v-model="form.pharmacyname" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="领取单位序号" prop="drugstore">
          <el-input v-model="form.drugstore" placeholder="请输入领取单位序号" />
        </el-form-item>
        <el-form-item label="经办人" prop="skull">
          <el-input v-model="form.skull" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="提取仓库" prop="warehouseid">
          <el-input v-model="form.warehouseid" placeholder="请输入提取仓库" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="售价" prop="sellingprice">
          <el-input v-model="form.sellingprice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="产地" prop="area">
          <el-input v-model="form.area" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="生产日期" prop="producedate">
          <el-date-picker clearable
            v-model="form.producedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期" prop="validdate">
          <el-date-picker clearable
            v-model="form.validdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数量" prop="drugstorenum">
          <el-input v-model="form.drugstorenum" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="批号" prop="skullbatch">
          <el-input v-model="form.skullbatch" placeholder="请输入批号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPharmacy, getPharmacy, delPharmacy, addPharmacy, updatePharmacy } from "@/api/system/pharmacy";

export default {
  name: "Pharmacy",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药房表格数据
      pharmacyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pharmacyname: null,
        drugstore: null,
        skull: null,
        warehouseid: null,
        unit: null,
        sellingprice: null,
        area: null,
        type: null,
        producedate: null,
        validdate: null,
        drugstorenum: null,
        skullbatch: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pharmacyname: [
          { required: true, message: "药品名称不能为空", trigger: "blur" }
        ],
        drugstore: [
          { required: true, message: "领取单位序号不能为空", trigger: "blur" }
        ],
        skull: [
          { required: true, message: "经办人不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "计量单位不能为空", trigger: "blur" }
        ],
        sellingprice: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "产地不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        producedate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        validdate: [
          { required: true, message: "有效期不能为空", trigger: "blur" }
        ],
        drugstorenum: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        skullbatch: [
          { required: true, message: "批号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询药房列表 */
    getList() {
      this.loading = true;
      listPharmacy(this.queryParams).then(response => {
        this.pharmacyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pharmacyid: null,
        pharmacyname: null,
        drugstore: null,
        skull: null,
        warehouseid: null,
        unit: null,
        sellingprice: null,
        area: null,
        type: null,
        producedate: null,
        validdate: null,
        drugstorenum: null,
        skullbatch: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pharmacyid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药房";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pharmacyid = row.pharmacyid || this.ids
      getPharmacy(pharmacyid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药房";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pharmacyid != null) {
            updatePharmacy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPharmacy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pharmacyids = row.pharmacyid || this.ids;
      this.$modal.confirm('是否确认删除药房编号为"' + pharmacyids + '"的数据项？').then(function() {
        return delPharmacy(pharmacyids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pharmacy/export', {
        ...this.queryParams
      }, `pharmacy_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
